package me.shurufa.activities;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.API;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Settings;
import me.shurufa.utils.Utils;

/**
 * 修改密码
 *
 * @FileName: me.shurufa.activities.ModifyPasswordActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 17:12
 */
public class ModifyPasswordActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.toast_text)
    TextView toastText;

    @Bind(R.id.oldpassword_edit)
    EditText oldpasswordEdit;

    @Bind(R.id.newpassword_edit)
    EditText newpasswordEdit;

    @Bind(R.id.confirm_edit)
    EditText confirmEdit;

    @Bind(R.id.modify_complete)
    Button modifyComplete;

    @Bind(R.id.right_text)
    TextView right_text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_modify_password;
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        titleTextView.setText(getString(R.string.modify_password));
        modifyComplete.setOnClickListener(this);
        right_text.setVisibility(View.VISIBLE);
        right_text.setText(getString(R.string.complete));
        right_text.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right_text:
            case R.id.modify_complete:
                if (isInputValid()){
                    final String password = newpasswordEdit.getText().toString();
                    final String oldPassword = oldpasswordEdit.getText().toString();
                    new RequestServerTask<BaseResponse>(BaseResponse.class,this,getString(R.string.commit_pls_wait)){

                        @Override
                        protected String requestServer() {
                            Map<String, Object> params = new HashMap<>();
                            params.put("token", Global.currentUser.token);
                            params.put("oldPassword", oldPassword);
                            params.put("password", password);
                            return HttpUtil.post(API.USER_MODIFY_PASSWORD, params);
                        }

                        @Override
                        protected void onSuccess(BaseResponse result) {
                            Utils.showToast(R.string.commit_success);
                            //删除用户 重新登录
                            EventBus.getDefault().post(new Intent(Constants.ACTION_USER_CHANGE));
                            Settings.delUser(String.valueOf(Global.currentUser.id));
                            Global.currentUser = null;
                            Intent intent = new Intent(ModifyPasswordActivity.this, NewLoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }.setDefaultErrMsg(R.string.commit_failed).start();
                }
                break;

        }
    }

    /**
     * 验证密码是否符合规则
     * @return
     */
    private boolean isInputValid(){
        String oldpwd = oldpasswordEdit.getText().toString();
        String newpwd = newpasswordEdit.getText().toString();
        String cfmpwd = confirmEdit.getText().toString();

        if (TextUtils.isEmpty(oldpwd)){
            Utils.showToast(R.string.old_password_can_not_empty);
            return false;
        }
        if (TextUtils.isEmpty(newpwd)&&TextUtils.isEmpty(cfmpwd)){
            Utils.showToast(R.string.pls_input_new_password);
            return false;
        }
        if (!newpwd.equals(cfmpwd)){
//            show anim
            doToast();
            return false;
        }
        return true;
    }

    private void doToast(){
        Utils.showToast(getString(R.string.new_password_not_same));
        toastText.setText(getString(R.string.new_password_not_same));
        toastText.setVisibility(View.VISIBLE);
        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.toast_anim);
        animator.setTarget(toastText);
        animator.start();
        mHandler.sendEmptyMessageDelayed(Constants.CODE_TOAST, 4000);
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what) {
                case Constants.CODE_TOAST:
                    toastText.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    };
}
